package esconceito



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class OffsetController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Offset.list(params), model:[offsetInstanceCount: Offset.count()]
    }

    def show(Offset offsetInstance) {
        respond offsetInstance
    }

    def create() {
        respond new Offset(params)
    }

    @Transactional
    def save(Offset offsetInstance) {
        if (offsetInstance == null) {
            notFound()
            return
        }

        if (offsetInstance.hasErrors()) {
            respond offsetInstance.errors, view:'create'
            return
        }

        offsetInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'offset.label', default: 'Offset'), offsetInstance.id])
                redirect offsetInstance
            }
            '*' { respond offsetInstance, [status: CREATED] }
        }
    }

    def edit(Offset offsetInstance) {
        respond offsetInstance
    }

    @Transactional
    def update(Offset offsetInstance) {
        if (offsetInstance == null) {
            notFound()
            return
        }

        if (offsetInstance.hasErrors()) {
            respond offsetInstance.errors, view:'edit'
            return
        }

        offsetInstance.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'Offset.label', default: 'Offset'), offsetInstance.id])
                redirect offsetInstance
            }
            '*'{ respond offsetInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(Offset offsetInstance) {

        if (offsetInstance == null) {
            notFound()
            return
        }

        offsetInstance.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Offset.label', default: 'Offset'), offsetInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'offset.label', default: 'Offset'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
